﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    /// <summary>
    /// 条件组件,此组将不用再集成，以后将和ConditionImage合并
    /// </summary>
    public abstract class ConditionControl : UserControl
    {
        protected UI.Part _part;
        protected ServiceProxy.DataServiceRef.DataResult _dataResult;
        protected ServiceProxy.DataServiceRef.DataSeries _dataSeries;
        protected ServiceProxy.DataServiceRef.DataItem _dataItem;
        protected Script.DataResultScriptObject _dataResultObj;
        protected Script.DataSeriesScriptObject _dataSeriesObj;
        protected Script.DataItemScriptObject _dataItemObj;

        /// <summary>
        /// 写一个表达式返回bool值，此表达式将在脚本引擎中运行，可以使用参数dataSeries, data
        ///
        /// </summary>
        [XProperty(XPropertyCategory.Other, "Condition", "条件")]
        public string Condition
        {
            get
            {
                return (string)GetValue(ConditionProperty);
            }
            set
            {
                SetValue(ConditionProperty, value);
            }
        }

        public static DependencyProperty ConditionProperty = DependencyProperty.Register("Condition",
            typeof(string),
            typeof(ConditionControl),
            null);

        public ConditionControl()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="part">控件所在的组件</param>
        /// <param name="dataItem">控件对应的数据项</param>
        internal void Init(UI.Part part,
            Script.DataSeriesScriptObject dataSeries, 
            ServiceProxy.DataServiceRef.DataItem dataItem)
        {
            this._part = part;
            this._dataSeriesObj = dataSeries;
            this._dataItem = dataItem;
        }

        internal void Init(UI.Part part,
            ServiceProxy.DataServiceRef.DataResult dataResult,
            ServiceProxy.DataServiceRef.DataSeries dataSeries,
            ServiceProxy.DataServiceRef.DataItem dataItem)
        {
            this._part = part;
            this._dataResult = dataResult;
            this._dataSeries = dataSeries;
            this._dataItem = dataItem;
        }

        public void ValidateCondition()
        {
            if (this._dataItem != null && string.IsNullOrEmpty(this._dataItem.Value))
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
                return;
            }

            // 检查组件是否可以显示
            ScriptEngine scriptEngine = this._part.PartContext.ScriptEngine;
            Script.DataItemScriptObject dataItemScriptObj = null;
            if (_dataItem != null)
            {
                dataItemScriptObj = new Script.DataItemScriptObject(_dataItem);
            }

            try
            {
                if (scriptEngine.RunDataExpressionToBoolean(this.Condition,
                    new Script.DataSeriesScriptObject(_dataSeries),
                    dataItemScriptObj) == true)
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                    OnDisplay();
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public virtual void SetProperty(string propertyName, object value)
        {
            if (propertyName == "Condition")
            {
                SetValue(ConditionProperty, value);
            }
        }

        protected abstract void OnDisplay();

        public void Deactive()
        {
            OnDeactive();
        }

        protected abstract void OnDeactive();
    }
}
